package com.zcg.gasmanager.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.zcg.gasmanager.dao.PayDao;
import com.zcg.gasmanager.model.User;
import com.zcg.gasmanager.utils.HibernateUtil;

@Component("payDao")
public class PayDaoImpl implements PayDao {
	
	private HibernateTemplate hibernateTemplate;

	/* (non-Javadoc)
	 * @see com.zcg.gasmanager.dao.impl.PayDao#update(com.zcg.gasmanager.model.User)
	 */
	@Override
	public void add(User u)  throws RuntimeException{
		List<User> users = hibernateTemplate.find("from User u where u.name = ?",u.getName());
		if(users.size() <= 0){
			return;
		}
		User user = (User) users.get(0);
		user.getGas().setBalance(
				u.getGas().getBalance() 
				+ user.getGas().getBalance());
		hibernateTemplate.update(user);
	}

	@Override
	public List<User> queryAll() throws RuntimeException {
		List<User> users = hibernateTemplate.find("from User");
		return users;
	}
	
	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}
	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
}
